package com.phospherus.user.service

import com.phospherus.common.service.impl.BaseServiceImpl
import com.phospherus.user.entity.Role
import com.phospherus.user.entity.RoleUser
import com.phospherus.user.entity.User
import com.phospherus.user.repository.RoleRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/11/30
 */
@Service
class RoleService : BaseServiceImpl<Role, Long, RoleRepository>() {
    @Autowired
    lateinit var roleUserService: RoleUserService
    @Autowired
    lateinit var userService: UserService

    fun bindUsers(roleId: Long, roleName: String, userIds: String?) {
        roleUserService.deleteByRoleId(roleId)
        if (userIds == null) {
            return
        } else {
            var uIds = userIds.split(",").mapTo(ArrayList(), { it.toLong() })
            if (uIds.isNotEmpty()) {
                var userList = userService.findAll(uIds)
                var roleUser = userList.mapTo(ArrayList(), { RoleUser(roleId = roleId, roleName = roleName, userId = it.id, userName = it.userName, account = it.account) })
                roleUserService.save(roleUser)
            }
        }
    }

    fun getRoleNameByRoleId(roleId: Long): String {
        return when (roleId) {
            1L -> "业务员"
            2L -> "业务员领导"
            3L -> "单证员"
            4L -> "单证领导"
            5L -> "风控员"
            6L -> "财务"
            else -> "管理员"
        }
    }

    fun getBindings(roleId: Long): List<User> {
        return userService.getBindingsUsers(roleId)
    }

    fun getUnBindings(roleId: Long): List<User> {
        return userService.getUnBindingsUsers(roleId)
    }

}